﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using eTMS.Controllers;

namespace eTMS.Forms.ManageList
{
    public partial class FrmGiaoVienImport : eTMS.Controls.BaseForm
    {
        public FrmGiaoVienImport()
        {
            InitializeComponent();
        }



        private void btnBrowser_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Filter = eTMS.Commons.Constants.FILTER_EXCEL_FILE;
            open.Multiselect = false;
            if (open.ShowDialog() == DialogResult.OK)
            {
                txtFileName.Text = open.FileName;
                DataTable tblSheet = Commons.ExcelReader.ReadExcellSheet(txtFileName.Text);
                List<string> ls = new List<string>();
                foreach (DataRow r in tblSheet.Rows)
                {
                    string st = r["TABLE_NAME"].ToString().Trim();
                    st = st.TrimStart('\'');
                    int index = st.IndexOf("$");
                    if (index > 0)
                    {
                        st = st.Substring(0, index);
                    }
                    st = st.TrimEnd('\'');
                    if (!ls.Contains(st))
                        ls.Add(st);
                }
                txtTenSheet.DataSource = ls;
            }
        }

        private void txtTenSheet_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (txtTenSheet.SelectedIndex >= 0)
            {
                DataTable tblColumns = Commons.ExcelReader.ReadExcellColumn(txtFileName.Text, txtTenSheet.Text);
                txtMaSo.DisplayMember = "COLUMN_NAME";
                txtMaSo.DataSource = tblColumns.Copy();
                txtDiaChi.DisplayMember = "COLUMN_NAME";
                txtDiaChi.DataSource = tblColumns.Copy();
                txtDienThoai.DisplayMember = "COLUMN_NAME";
                txtDienThoai.DataSource = tblColumns.Copy();
                txtEmail.DisplayMember = "COLUMN_NAME";
                txtEmail.DataSource = tblColumns.Copy();
                txtGioiTinh.DisplayMember = "COLUMN_NAME";
                txtGioiTinh.DataSource = tblColumns.Copy();
                txtHoTen.DisplayMember = "COLUMN_NAME";
                txtHoTen.DataSource = tblColumns.Copy();
                txtNgaySinh.DisplayMember = "COLUMN_NAME";
                txtNgaySinh.DataSource = tblColumns.Copy();
            }
        }
        GiaoVienController controller;
        private void btnRead_Click(object sender, EventArgs e)
        {
            controller.ResetTable();
            controller.Table.Columns.Add("TenGioiTinh");
            DataSet ds = Commons.ExcelReader.ReadExcellFile(txtFileName.Text, txtTenSheet.Text);

            if (ds.Tables.Count > 0)
            {
                DataTable tblTemp = ds.Tables[0];
                foreach (DataRow r in tblTemp.Rows)
                {
                    DataRow row = controller.Table.NewRow();
                    row["GIAOVIEN_ID"] = 0;
                    if (tblTemp.Columns.Contains(txtMaSo.Text))
                    {
                        row["MAGIAOVIEN"] = r[txtMaSo.Text];
                    }
                    if (tblTemp.Columns.Contains(txtHoTen.Text))
                    {
                        row["HoTenGiaoVien"] = r[txtHoTen.Text];
                    }
                    if (tblTemp.Columns.Contains(txtNgaySinh.Text))
                    {
                        if (txtNgaySinhFormat.Text.Trim().Length > 0)
                        {
                            DateTime date;
                            DateTime.TryParseExact(r[txtNgaySinh.Text].ToString(), txtNgaySinhFormat.Text, null, System.Globalization.DateTimeStyles.None, out date);
                            row["NgaySinh"] = date;
                        }
                    }
                    if (tblTemp.Columns.Contains(txtDiaChi.Text))
                    {
                        row["DiaChi"] = r[txtDiaChi.Text];
                    }
                    if (tblTemp.Columns.Contains(txtDienThoai.Text))
                    {
                        row["DienThoai"] = r[txtDienThoai.Text];
                    }
                    if (tblTemp.Columns.Contains(txtGioiTinh.Text))
                    {
                        bool isNu = r[txtGioiTinh.Text].ToString().Trim().ToLower() == txtNu.Text.Trim().ToLower();
                        row["TenGioiTinh"] = isNu ? "Nữ" : "Nam";
                        row["GioiTinh"] = isNu ? 0 : 1;
                    }
                    else
                    {
                        row["TenGioiTinh"] = "";
                        row["GioiTinh"] = 2;
                    }
                    row["BoMon_ID"] = comboBoxBoMon1.SelectedValue;
                    controller.Table.Rows.Add(row);
                }
            }
            controller.AddIndexToTable();
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = controller.Table;
        }

        private void FrmGiaoVienImport_Load(object sender, EventArgs e)
        {
            KhoaController.LoadAllKhoa(false);
            BoMonController.LoadAllBoMon(false);
            comboBoxKhoa1.BindData();
            controller = new GiaoVienController();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (controller.SaveGiaoVienTable())
            {
                ShowInfo("Bạn đã lưu danh sách giáo viên thành công!");
                controller.ResetTable();
                dataGridView1.AutoGenerateColumns = false;
                dataGridView1.DataSource = controller.Table;
            }
            else
            {
                ShowWarning("Có lỗi xảy ra khi lưu dữ liệu!");
            }
        }

        private void comboBoxKhoa1_SelectedValueChanged(object sender, EventArgs e)
        {
            comboBoxBoMon1.ClearSearchValue();
            if (comboBoxKhoa1.SelectedIndex > 0)
            {
                comboBoxBoMon1.AddSearchValues("Khoa_ID", comboBoxKhoa1.SelectedValue);
            }
            comboBoxBoMon1.BindData();
        }
    }
}
